package week4.day25;

import java.io.*;

/**
 * @Author fengzihao
 * @Date 2024-05-09 19:06
 */
public class CopyByThread1 {
    public static void main(String[] args) {
        CopyFileThread copyFileThread1 = new CopyFileThread(new File("txtfile/1.txt"), new File("txtfile/4.txt"),"线程1");
        CopyFileThread copyFileThread2 = new CopyFileThread(new File("txtfile/2.txt"), new File("txtfile/5.txt"),"线程2");
        CopyFileThread copyFileThread3 = new CopyFileThread(new File("txtfile/3.txt"), new File("txtfile/6.txt"),"线程3");
        copyFileThread1.start();
        copyFileThread2.start();
        copyFileThread3.start();
    }

}

class CopyFileThread extends Thread {
    private File sourceFile;
    private File targetFile;

    public CopyFileThread(File sourceFile, File targetFile,String name) {
        super(name);
        this.sourceFile = sourceFile;
        this.targetFile = targetFile;
    }

    @Override
    public void run() {
        long fileSize = sourceFile.length();
        long inFileSize = 0;
        try {
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            FileOutputStream fileOutputStream = new FileOutputStream(targetFile, true);
            int ch = -1;
            while ((ch = fileInputStream.read()) != -1) {
                fileOutputStream.write(ch);
                inFileSize++;
                System.out.println(sourceFile.getName() + "文件已经复制" + (inFileSize * 100.0) / fileSize + "%");
            }
            System.out.println(sourceFile.getName() + "复制完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}